from wsgiref.simple_server import make_server


def application(environ, start_response):
    # 按照http请求协议解析数据：environ
    # 按照http响应协议组装数据：start_response
    print(environ)
    print(type(environ)) # <class 'dict'>

    # 当前的请求路径
    path = environ.get('PATH_INFO')

    #start_response('200 OK', [('Content-Type', 'text/html')])
    start_response('200 OK', [])
    data=''
    if path == '/login':
        with open('login.html', 'r', encoding='utf8') as f:
            data = f.read()
    elif path == '/index':
        with open('index.html', 'r', encoding='utf8') as f:
            data = f.read()
    elif path == '/favicon.ico':
        with open('favicon.ico', 'rb') as f:
            data = f.read()
        return [data]

    #return [b'<h1>hello web</h1>']
    return [data.encode('utf8')]



# 封装socket
httpd = make_server('', 8800, application)

print('Serving HTTP on port 8800...')
# 等待用户连接：conn, addr = sock.accept()
httpd.serve_forever()

